#ifndef GPSCONTROLLER_H
#define GPSCONTROLLER_H
#include <QtCore/QObject>
#include <QByteArray>
#include "info.h"

class QString;
class AbstractSerial;
class SerialDeviceEnumerator;

enum CoordType
{
    GOOGLE           = 0,
    MERKATOR         = 1,
    MERKATOR_SPHERIC = 2
};

class gpsController : public QObject
{
    Q_OBJECT

    AbstractSerial * COMport;
    SerialDeviceEnumerator * COMportEnum;

    QByteArray buffer;
    int rrto; //timeout for ready read, msec
    int len;  //length data for read, bytes

    bool loopStopped;
    bool gpsDeviceFound;
    bool needReconnect;
    bool loopFinished;

protected:
    QString getCOMport();
    bool findGPSdevice();
    bool openCOMport( bool onDiscovery = false );
    void closeCOMport();
    bool initCOMport( bool onDiscovery = false );
    void loopXYZ();
    inline void loopStop() { loopStopped = true; }
    inline void loopStart() { loopStopped = false; }
    void DegMinSec2Decimal( nmeaINFO & in_info, double & out_x, double & out_y, double & out_z, CoordType t = GOOGLE );
    void Reconnect();

public:
    gpsController( QObject *parent = 0 );
    ~gpsController();

    inline int getTimeout() { return rrto; }
    inline void setTimeout( int & to ) { rrto = to; }
    inline int getDataLenForReadyRead() { return len; }
    inline void setDataLenForReadyRead( int & l ) { len = l; }

    void CreateCOMport();
    void Start();
    void Stop();
    bool ReadyForLoop();
    bool LookingForGPS();

public slots:
    void ctl_delete_COMport();

signals:
    void ctl_sendXYZ( double x, double y, double z );
    void ctl_started();
    void ctl_finished();
    void ctl_gpsNotFound();
    void ctl_COMport_removed();

private slots:
    void CheckNeedReconnect();

};

#endif // GPSCONTROLLER_H
